﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Memo.Electricity.Engine;
using UnityEngine.SceneManagement;
using System.Collections;

namespace Memo.Electricity.Game
{

	public class CLevelMgr : CSingletonBehaviour<CLevelMgr>
	{
		public struct LevelInfo
		{
			public int majorId;
			public int minorId;
			public string LevelName { get => $"Level{majorId}_{minorId}";}
		}

		private HashSet<LevelInfo> mLevels;
		private LevelInfo mCurrentLevel;
		private string mStartSceneName;

		#region 对外接口

		public LevelInfo CurrentLevel { get => mCurrentLevel; }

		public void GoToLevel(int majorId, int minorId)
		{
			OnLevelChanged();
			mCurrentLevel.majorId = majorId;
			mCurrentLevel.minorId = minorId;
			CSceneMgr.Inst.LoadScene(CurrentLevel.LevelName);
		}

		public void GoToNextLevel()
		{
			OnLevelChanged();
			LevelInfo info = mCurrentLevel;
			info.minorId++;
			if (!mLevels.Contains(info))
			{
				info.majorId++;
				info.minorId = 1;
				if (!mLevels.Contains(info))
				{
					BackToStartScene();
					return;
				}
			}
			mCurrentLevel = info;
			CSceneMgr.Inst.LoadScene(CurrentLevel.LevelName);
		}

		public void RestartCurrentLevel()
		{
			OnLevelChanged();
			CSceneMgr.Inst.LoadScene(CurrentLevel.LevelName);
		}
		public void BackToStartScene()
		{
			OnLevelChanged();
			CSceneMgr.Inst.LoadScene(mStartSceneName);
		}

		#endregion

		private void Awake()
		{
			GenLevelInfo();
			mStartSceneName = SceneUtility.GetScenePathByBuildIndex(1);
			mStartSceneName = mStartSceneName.Substring(mStartSceneName.LastIndexOf("/") + 1);
			mStartSceneName = mStartSceneName.Remove(mStartSceneName.LastIndexOf(".unity"));

			//Event
			CEventMgr.Inst.AddListener(EEventType.OnPlayerDied, () => CCoroutineMgr.Inst.StartCoroutine(OnPlayerDied()));
			CEventMgr.Inst.AddListener(EEventType.OnPlayerWin, () => CCoroutineMgr.Inst.StartCoroutine(OnPlayerWin()));
		}

		private void OnLevelChanged()
		{
			CRelay.Destroy();
		}

		private void GenLevelInfo()
		{
			mLevels = new HashSet<LevelInfo>();
			int sceneCount = SceneManager.sceneCountInBuildSettings;
			for (int i = 0; i < sceneCount; i++)
			{
				string name = SceneUtility.GetScenePathByBuildIndex(i);
				name = name.Substring(name.LastIndexOf("/") + 1);
				name = name.Remove(name.LastIndexOf(".unity"));
				if (name.LastIndexOf("Level") != -1 )
				{
					int begin = name.LastIndexOf("Level");
					string[] strs = name.Substring(begin + 5).Split('_');
					if (strs.Length == 2)
					{
						LevelInfo info = new LevelInfo();
						info.majorId = int.Parse(strs[0]);
						info.minorId = int.Parse(strs[1]);
						mLevels.Add(info);
					}
				}
			}

			mCurrentLevel.majorId = 1;
			mCurrentLevel.minorId = 1;
		}

		#region Event Handler

		private IEnumerator OnPlayerDied()
		{
			//yield return new WaitForSeconds(1.0f);
			yield return null;
			RestartCurrentLevel();
		}

		private IEnumerator OnPlayerWin()
		{
			yield return new WaitForSeconds(1.0f);
			GoToNextLevel();
		}

		#endregion


	}
}